package controller

import (
	"gin_tem/internal/dto"
	"gin_tem/internal/service"
	"gin_tem/utils"
	"github.com/gin-gonic/gin"
)

// UserController 用户controller
type UserController struct {
	userService service.UserService
}

func NewUserController(userService service.UserService) *UserController {
	return &UserController{userService: userService}
}

func (uc *UserController) CreateUser(c *gin.Context) {
	var req dto.CreateUserRequest
	if err := utils.DefaultValidator(c, &req); err != nil {
		c.JSON(200, gin.H{
			"code": 1,
			"msg":  err.Error(),
		})
		return
	}
	err := uc.userService.CreateUser(&req)
	if err != nil {
		c.JSON(200, gin.H{"code": 1, "msg": err.Error()})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"msg":  "注册成功!",
	})
}

func (uc *UserController) Login(c *gin.Context) {
	var req dto.LoginUserRequest
	if err := utils.DefaultValidator(c, &req); err != nil {

	}
	response, err := uc.userService.Login(&req)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": response,
	})
}

func (uc *UserController) List(c *gin.Context) {
	list, error := uc.userService.List()
	if error != nil {
		c.JSON(500, gin.H{
			"code": 500,
			"msg":  error.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": list,
	})
}
